package cn.dm.controller;

import cn.dm.common.Dto;
import cn.dm.common.DtoUtil;
import cn.dm.common.EmptyUtils;
import cn.dm.pojo.DmUser;
import cn.dm.service.LocalUserService;
import cn.dm.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.regex.Pattern;

@RestController
@Slf4j
public class UserController {

    @Resource
    private SmsService smsService;
    @Resource
    private LocalUserService localUserService;


    /**
     * 获取注册验证码接口
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping("/user/api/p/code")
    public Dto getCode(@RequestBody Map<String, Object> param) throws Exception {
        log.info("=====>> 获取注册验证码接口开始执行");
        String phone = (String) param.get("phone");
        log.info("=====>> 传入的值："+phone);
        //判断该手机号是否已经被注册
        DmUser dmUser = localUserService.getDmUserListByMap(phone);
        if(EmptyUtils.isEmpty(dmUser)){
            //没有注册的情况下，判断输入的是邮箱还是手机号
            if(this.validPhone(phone) || this.validEmail(phone)==false){
                //手机号
                DmUser dmUser1 = new DmUser();
                dmUser1.setPhone(phone);
                if(localUserService.registerUserByPhone(dmUser1)){
                    return DtoUtil.returnSuccess("验证码已发送");
                }else {
                    return DtoUtil.returnFail("验证码发送失败","1007");
                }
            }else if(this.validPhone(phone)==false || this.validEmail(phone)){
                //邮箱
            }else if(this.validPhone(phone)==false && this.validEmail(phone)==false){
                return DtoUtil.returnFail("输入的账号格式错误","1001");
            }
        }else{
            log.info(phone+"该手机号或邮箱已被注册");
            return DtoUtil.returnFail("该手机号或邮箱已被注册","1003");
        }
        return DtoUtil.returnFail("-1","错误");
    }

    /**
     * 验证是否合法的手机号
     * @param phone
     * @return
     */
    private boolean validPhone(String phone) {
        String regex="^1[3578]{1}\\d{9}$";
        return Pattern.compile(regex).matcher(phone).find();
    }


    /** 验证邮箱格式是否正确
     * 合法 E-mail 地址：
     * 1. 必须包含一个并且只有一个符号“@”
     * 2. 第一个字符不得是“@”或者“.”
     * 3. 不允许出现“@.”或者.@
     * 4. 结尾不得是字符“@”或者“.”
     * 5. 允许“@”前的字符中出现“＋”
     * 6. 不允许“＋”在最前面，或者“＋@”
     */
    private boolean validEmail(String email){
        String regex="^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$" ;
        return Pattern.compile(regex).matcher(email).find();
    }


    /**
     * 验证手机号是否可用接口
     * @param param
     * @return
     */
    @PostMapping("/user/api/p/checkEmail")
    public Dto verifyAccountAvailability(@RequestBody Map<String, Object> param) throws Exception {
        log.info("=====>> 验证手机号是否可用接口开始执行");
        String phone = (String) param.get("phone");
        log.info("=====>> 传入的值："+phone);
        if(this.validPhone(phone)==false && this.validEmail(phone)==false){
            return DtoUtil.returnFail("输入的账号格式错误","1001");
        }else{
            //判断该手机号是否已经被注册
            DmUser dmUser = localUserService.getDmUserListByMap(phone);
            if(EmptyUtils.isEmpty(dmUser)){
                return  DtoUtil.returnSuccess("手机号或邮箱未被注册");
            }else {
                log.info(phone+"该手机号或邮箱已被注册");
                return DtoUtil.returnFail("该手机号或邮箱已被注册","1003");
            }
        }
    }

    /**
     * ### 使用邮箱/手机号注册接口
     *
     * >本接口用于使用手机号成功接收验证码后，提交注册
     * @param dmUser
     * @param vscode
     * @return
     * @throws Exception
     */
    @RequestMapping("/user/api/p/register")
    public Dto userRegistration(@RequestBody DmUser dmUser,@RequestParam String vscode) throws Exception{
        log.info("=====>> 使用邮箱/手机号注册接口接口开始执行");
        log.info("=====>> 接收参数："+dmUser.getPhone()+",："+dmUser.getPassword()+",："+vscode);
        if(this.validPhone(dmUser.getPhone())==false && this.validEmail(dmUser.getPhone())==false){
            return DtoUtil.returnFail("输入的账号格式错误","1001");
        }else {
            //判断该手机号是否已经被注册
            DmUser dmUser1 = localUserService.getDmUserListByMap(dmUser.getPhone());
            if(EmptyUtils.isEmpty(dmUser1)){
                int i = localUserService.userRegistration(dmUser,vscode);
                if(i==-1){
                    return  DtoUtil.returnFail("验证码错误","1004");
                }else if(i==1){
                    return DtoUtil.returnSuccess("注册成功");
                }else {
                    return DtoUtil.returnFail("-1","错误");
                }
            }else {
                return DtoUtil.returnFail("该手机号或邮箱已被注册","1003");
            }

        }

    }
}
